# coding: utf-8
# @Author: 吴启新

import os
from typing import Optional
from src.file.selector import get_data_file_path


def get_file_path(filename: str) -> str:
    """获取文件完整路径的公共函数"""
    data_dir = get_data_file_path()
    return os.path.join(data_dir, filename)


def number_to_excel_column(num: int) -> str:
    """将数字转换为Excel列字母表示法"""
    result = ""
    while num >= 0:
        result = chr(num % 26 + ord('A')) + result
        num = num // 26 - 1
        if num < 0:
            break
    return result


def get_user_confirmation(prompt: str) -> bool:
    """获取用户确认"""
    user_input = input(prompt).strip()
    return not user_input  # 用户直接按回车表示确认


def validate_and_update_class_count(num_classes: int) -> Optional[int]:
    """验证并更新班级数量"""
    print(f"自动统计班级数量为: {num_classes}")
    input_num_classes = input("班级数量是否正确？（正确按回车，如错误请输入正确的班级数）").strip()
    
    if input_num_classes and input_num_classes.strip().isdigit():
        return int(input_num_classes)
    elif input_num_classes and not input_num_classes.isdigit():
        print("输入的班级数量格式有误")
        return None
    print(f"最终确定班级数量为: {num_classes}")
    return num_classes


def display_subjects_info(auto_subjects_info: list[dict], number_to_excel_column_func) -> None:
    """显示科目信息"""
    print("自动统计考试科目为:")
    for i, subject_info in enumerate(auto_subjects_info, 1):
        print(
            f"  {i}. {subject_info['subject']} (位置: 第{subject_info['row'] + 1}行, 第{number_to_excel_column_func(subject_info['col'])}列)")